# UpdateVersion.ps1

# 版本号文件路径（.csproj）
$csprojPath = "$PSScriptRoot\FundHelper.csproj"  # 修改为你的 .csproj 文件名
#$versionPattern = '(?<=<Version>)(\d+\.\d+\.\d+\.)(\d+)(?=</Version>)'
$assemblyVersionPattern = '(?<=<AssemblyVersion>)(\d+\.\d+\.\d+\.)(\d+)(?=</AssemblyVersion>)'
$fileVersionPattern = '(?<=<FileVersion>)(\d+\.\d+\.\d+\.)(\d+)(?=</FileVersion>)'

# 检查文件是否存在
if (-Not (Test-Path $csprojPath)) {
    Write-Host "CSProj file not found!"
    exit 1
}

# 读取文件内容
$content = Get-Content $csprojPath

# 提取当前的版本号

$fileVersionMatch = [regex]::Match($content, $fileVersionPattern)

if ($fileVersionMatch.Success) {
    # 提取当前构建号
    $buildNumber = [int]$fileVersionMatch.Groups[2].Value

    # 递增版本号
    $newBuildNumber = $buildNumber + 1

    # 更新版本号
    #$newContent = $content -replace $versionPattern, "$($versionMatch.Groups[1].Value)$newBuildNumber"
    #$newContent = $newContent -replace $assemblyVersionPattern, "$($assemblyVersionMatch.Groups[1].Value)$newBuildNumber"
    $newContent = $content  -replace $fileVersionPattern, "$($fileVersionMatch.Groups[1].Value)$newBuildNumber"

    # 写入更新后的内容
    Set-Content $csprojPath $newContent
    Write-Host "Updated version to $($fileVersionMatch.Groups[1].Value)$newBuildNumber"
} else {
    Write-Host "Version information not found in the .csproj file"
    exit 1
}
